package top.chmin.project.cloud.common.entity.auto;

// field import auto
// #bind.field.tableCode#

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import top.chmin.project.cloud.common.entity.Page;
import top.chmin.project.cloud.common.tool.DatetimeTool;
import top.chmin.project.cloud.common.tool.LocalTool;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * chmin
 * 2023-12-26 19:45:33
 * cloud-builder
 */
@Slf4j
@Component
public class EntityPlugin {

    public void checkSave(EntitySave.Param param) {
        log.debug("checkSave-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("保存对象不能为空");
        }
    }

    public void beforeSave(Entity entity) {
        log.debug("beforeSave-entity: {}", JSONUtil.toJsonStr(entity));
        entity.setCreateBy(LocalTool.getUserId());
        entity.setUpdateBy(LocalTool.getUserId());
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());

        if (StringUtils.isEmpty(entity.getPrefix())) {
            entity.setPrefix("");
        }

        if (StringUtils.isEmpty(entity.getClassName())) {
            entity.setClassName(
                    Stream.of(
                                    (
                                            entity.getTableName().startsWith(entity.getPrefix())
                                                    ? entity.getTableName().substring(entity.getPrefix().length())
                                                    : entity.getTableName()
                                    )
                                            .split("_")
                            )
                            .map(StringUtils::capitalize)
                            .collect(Collectors.joining())

            );
        }
        if (StringUtils.isEmpty(entity.getPack())) {
            entity.setPack(
                    (
                            entity.getTableName().startsWith(entity.getPrefix())
                                    ? entity.getTableName().substring(entity.getPrefix().length())
                                    : entity.getTableName()
                    )
                            .replace("_", ".")
            );
        }
        if (StringUtils.isEmpty(entity.getPath())) {
            entity.setPath(
                    (
                            entity.getTableName().startsWith(entity.getPrefix())
                                    ? entity.getTableName().substring(entity.getPrefix().length())
                                    : entity.getTableName()
                    )
                            .replace("_", "\\")
            );
        }
    }

    public void afterSave(EntitySave.Param param, Entity entity, int result) {
        log.debug("afterSave-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSave-entity: {}", JSONUtil.toJsonStr(entity));
        log.debug("afterSave-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkUpdateById(EntityUpdateById.Param param) {
        log.debug("checkUpdateById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("更新对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("更新对象id不能为空");
        }
    }

    public void beforeUpdateById(Entity entity) {
        log.debug("beforeUpdateById-entity: {}", JSONUtil.toJsonStr(entity));
        entity.setUpdateBy(LocalTool.getUserId());
        entity.setUpdateTime(LocalDateTime.now());
    }

    public void afterUpdateById(EntityUpdateById.Param param, Entity entity, int result) {
        log.debug("afterUpdateById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterUpdateById-entity: {}", JSONUtil.toJsonStr(entity));
        log.debug("afterUpdateById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkDeleteById(EntityDeleteById.Param param) {
        log.debug("checkDeleteById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("删除对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("删除对象id不能为空");
        }
    }

    public void afterDeleteById(EntityDeleteById.Param param, int result) {
        log.debug("afterDeleteById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterDeleteById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelectById(EntitySelectById.Param param) {
        log.debug("checkSelectById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("查询对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("查询对象id不能为空");
        }
    }

    public void beforeSelectById(EntitySelectById.Param param, EntityFPC fpc) {
        log.debug("beforeSelectById-param: {}", JSONUtil.toJsonStr(param));

    }

    public void afterSelectById(EntitySelectById.Param param, EntitySelectById.Result result) {
        log.debug("afterSelectById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelectPage(EntitySelectPage.Param param) {
        log.debug("checkSelectPage-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("分页对象不能为空");
        }
    }

    public void beforeSelectPage(EntitySelectPage.Param param, EntityFPC fpc) {
        log.debug("beforeSelectPage-param: {}", JSONUtil.toJsonStr(param));
// field beforeSelectPage auto
// #bind.field.tableCode#
        fpc.tableCode().eq(param.getTableCode());
// $bind.field.tableCode$

// #bind.field.createBy#
        fpc.createBy().eq(param.getCreateBy());
// $bind.field.createBy$

// #bind.field.updateBy#
        fpc.updateBy().eq(param.getUpdateBy());
// $bind.field.updateBy$

// #bind.field.createTime#
        fpc.createTime().ge(param.getCreateTime() == null || param.getCreateTime().length == 0 ? null : param.getCreateTime()[0]);
        fpc.createTime().lt(param.getCreateTime() == null || param.getCreateTime().length < 2 ? null : DatetimeTool.nextDay(param.getCreateTime()[1]));
// $bind.field.createTime$

// #bind.field.updateTime#
        fpc.updateTime().ge(param.getUpdateTime() == null || param.getUpdateTime().length == 0 ? null : param.getUpdateTime()[0]);
        fpc.updateTime().lt(param.getUpdateTime() == null || param.getUpdateTime().length < 2 ? null : DatetimeTool.nextDay(param.getUpdateTime()[1]));
// $bind.field.updateTime$

// #bind.field.tableName#
        fpc.tableName().eq(param.getTableName());
// $bind.field.tableName$

// #bind.field.prefix#
        fpc.prefix().eq(param.getPrefix());
// $bind.field.prefix$

// #bind.field.className#
        fpc.className().eq(param.getClassName());
// $bind.field.className$

// #bind.field.module#
        fpc.module().eq(param.getModule());
// $bind.field.module$

// #bind.field.pack#
        fpc.pack().eq(param.getPack());
// $bind.field.pack$

// #bind.field.path#
        fpc.path().eq(param.getPath());
// $bind.field.path$

// #bind.field.remark#
        fpc.remark().eq(param.getRemark());
// $bind.field.remark$

    }

    public void afterSelectPage(EntitySelectPage.Param param, Page<EntitySelectPage.Result> result) {
        log.debug("afterSelectPage-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectPage-result: {}", JSONUtil.toJsonStr(result));

    }

    // entity select auto


    public void checkSelectTableCode(EntityTableCodeSelect.Param param) {
        log.debug("checkSelectTableCode-param: {}", JSONUtil.toJsonStr(param));
    }

    public void beforeSelectTableCode(EntityTableCodeSelect.Param param, EntityFPC fpc) {
        log.debug("beforeSelectTableCode-param: {}", JSONUtil.toJsonStr(param));
        log.debug("beforeSelectTableCode-fpc: {}", JSONUtil.toJsonStr(fpc));

    }

    public void afterSelectTableCode(EntityTableCodeSelect.Param param, List<EntityTableCodeSelect.Result> result) {
        log.debug("afterSelectTableCode-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectTableCode-result: {}", JSONUtil.toJsonStr(result));

    }


// entity search auto
}
